<?php


namespace Frieza\Modules\Search\Controllers;


use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;


class SearchFeedbackController extends Controller
{


    public function index(Request $request, Content $content)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('per_page', 15);

        $list = \Biz\service\activity\SearchFeedback::admList($page, $pageSize);

        $gridModel = new GridDataModel($list, \Biz\model\activity\SearchFeedback::count());

        $grid = Admin::grid($gridModel, function (Grid $grid) use ($gridModel) {
//            $grid->filter(function (Grid\Filter $filter) {
//                $filter->disableIdFilter();
//                $filter->column(8, function ($filter) {
//                    $filter->equal('keyword', '关键字')->placeholder("请输入关键字");
//                });
//                $filter->expand();
//            });

            $scoreMap = array_column(\Biz\service\activity\SearchFeedback::$score, 'title', 'id');
            $reasonMap = array_column(\Biz\service\activity\SearchFeedback::$reason, 'title', 'id');

            $grid->disableFilter();
            $grid->disableRowSelector();
            $grid->disableExport();
            $grid->paginate($gridModel->getPerPage()); //页面显示条数
            $grid->column('uid', '用户id');
            $grid->column('input_query', '搜索词');
            $grid->column('feedback_score', '打分')->using($scoreMap);
            $grid->column('feedback_reason', '存在的问题')->display(function () use ($reasonMap) {
                $arr = json_decode($this->feedback_reason, true);
                if (!is_array($arr)) return $this->feedback_reason;
                sort($arr);
                $name = [];
                if ($arr) {
                    foreach ($arr as $v) {
                        $name[] = $reasonMap[$v] ?? '-';
                    }
                }

                return implode("<br/>", $name);
            });
            $grid->column('feedback_other', '其他问题和意见');
            $grid->column('addtime', '提交时间');
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
            });
        });

        return $content
            ->header('搜索反馈列表')
            ->row($grid);
    }

}